#!/usr/bin/env python
#encoding=utf-8

import sys
import string
import random

from matplotlib import pyplot as plt

from Graph import Graph, Vertex, Edge

class RandomGraph(Graph):
	""" Erdos Renyi model
	"""

	def add_random_edges(self, p):
		""" add random edge for every two vertices with possibility p.
		"""
		vertices = self.vertices()
		for i, v in enumerate(vertices):
			for j, w in enumerate(vertices):
				if j >= i: break
				if random.random() > p: continue
				self.add_edge(Edge(v, w))

def test_graph(n, p):
	labels = string.ascii_uppercase + string.ascii_lowercase + string.punctuation
	vs = [Vertex(i) for i in labels[:n]]
	g = RandomGraph(vs)
	g.add_random_edges(p)
	return g.is_connected()

def test_p(n, p, num):
	count = 0
	for i in range(num):
		if test_graph(n, p):
			count += 1
	return count

def main(n = 26, num = 1):
	n = int(n)
	num = int(num)

	xs, ys = [], []
	p = 2
	while p < 100:
		xs.append(p)
		ys.append(test_p(n, p/100, num))
		print(n, p, num)
		p += 2

	plt.xkcd()
	plt.plot(xs, ys)
	plt.show()


if __name__ == '__main__':
	main(*sys.argv[1:])